﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using Com.DAO;

namespace WFService
{
    /// <summary>
    /// WebService1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消注释以下行。 
    // [System.Web.Script.Services.ScriptService]
    public class WorkflowHelper : System.Web.Services.WebService
    {
        private DataManager dataManager = new DataManager();

        private string TransferApprovalXml(string approvalXml)
        {
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.LoadXml(approvalXml);
            System.Xml.XmlNode node = doc.SelectSingleNode("Root/Approvals");
            return node.InnerXml;
        }

        [WebMethod(Description = "获取上级人员的信息，根据职位ID")]
        public DataSet GetManagerByPositionID(string positionID)
        {
            try
            {
                if (this.dataManager.IsClosed)
                {
                    this.dataManager.OpenWithConfig();
                }

                string sql = string.Format(" EXEC PR_GetManagerByPositionID '{0}'", positionID);
                DataSet ds = this.dataManager.SelectDataSet(sql);

                if (ds.Tables[0].Rows.Count == 0)
                {
                    throw new Exception(String.Format("找不到职位[ID:{0}]的上级人员信息。", positionID));
                }
                return ds;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                try
                {
                    if (this.dataManager != null && !this.dataManager.IsClosed)
                    {
                        this.dataManager.Close();
                    }
                }
                catch
                {
                }
            }
        }

        [WebMethod(Description = "获取上级人员的信息，根据组织单元ID")]
        public DataSet GetManagerByDeptID(string deptID)
        {
            try
            {
                if (this.dataManager.IsClosed)
                {
                    this.dataManager.OpenWithConfig();
                }

                string sql = string.Format(" EXEC PR_GetManagerByDeptID '{0}'", deptID);
                DataSet ds = this.dataManager.SelectDataSet(sql);

                if (ds.Tables[0].Rows.Count == 0)
                {
                    throw new Exception(String.Format("找不到部门[ID:{0}]的上级人员信息。", deptID));
                }
                return ds;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                try
                {
                    if (this.dataManager != null && !this.dataManager.IsClosed)
                    {
                        this.dataManager.Close();
                    }
                }
                catch
                {
                }
            }
        }

        [WebMethod(Description = "检查是否某个流程的顶级职位，用于判断是否跳出逐步审批(0否，1是)")]
        public int CheckIsTopPosition(string applyType, int jobLevel, string positionID)
        {
            int deptKind = this.Get_DeptKindByPositionID(positionID);
            return Check_IsTopPosition(applyType, deptKind, jobLevel, positionID);
        }

        /// <summary>
        /// 检查是否某个流程的顶级职位，用于判断是否跳出逐步审批(1是，0否)
        /// </summary>
        /// <param name="applyType">流程类型</param>
        /// <param name="deptKind">0非营，1工商铺，2二级市场，3三级市场</param>
        /// <param name="positionID">职位ID</param>
        /// <returns></returns>
        private int Check_IsTopPosition(string applyType, int deptKind, int jobLevel, string positionID)
        {
            int result = 0;
            try
            {
                if (this.dataManager.IsClosed)
                {
                    this.dataManager.OpenWithConfig();
                }
                string sql = string.Format(" EXEC PR_CheckIsTopPosition '{0}','{1}','{2}','{3}'", applyType, deptKind.ToString(), jobLevel.ToString(), positionID);
                DataSet ds = this.dataManager.SelectDataSet(sql);

                if (ds.Tables[0].Rows.Count == 0)
                {
                    result = 0;
                }
                else
                {
                    result = int.Parse(ds.Tables[0].Rows[0]["istop"].ToString());
                    if (result > 1)
                    {
                        result = 1;
                    }
                }
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                try
                {
                    if (this.dataManager != null && !this.dataManager.IsClosed)
                    {
                        this.dataManager.Close();
                    }
                }
                catch
                {
                }
            }
        }

        [WebMethod(Description = "获取部门类型，根据职位ID(0非营，1工商铺，2二级市场，3三级市场)")]
        public int GetDeptKindByPositionID(string positionID)
        {
            return Get_DeptKindByPositionID(positionID);
        }

        [WebMethod(Description = "获取部门类型，根据组织单元ID(0非营，1工商铺，2二级市场，3三级市场)")]
        public int GetDeptKindByDeptID(string deptID)
        {
            return Get_DeptKindByDeptID(deptID);
        }

        #region private method
        private int Get_DeptKindByPositionID(string positionID)
        {
            string deptID = this.Get_DeptIDByPositionID(positionID);
            return this.Get_DeptKindByDeptID(deptID);
        }

        private int Get_DeptKindByDeptID(string deptID)
        {
            int result = -1;
            try
            {
                if (this.dataManager.IsClosed)
                {
                    this.dataManager.OpenWithConfig();
                }
                string sql = string.Format(" EXEC PR_GetDeptKindByDeptID '{0}'", deptID);
                DataSet ds = this.dataManager.SelectDataSet(sql);

                if (ds.Tables[0].Rows.Count == 0)
                {
                    result = 0;
                }
                else
                {
                    result = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                try
                {
                    if (this.dataManager != null && !this.dataManager.IsClosed)
                    {
                        this.dataManager.Close();
                    }
                }
                catch
                {
                }
            }
        }

        private string Get_DeptIDByPositionID(string positionID)
        {
            string result = "";
            try
            {
                if (this.dataManager.IsClosed)
                {
                    this.dataManager.OpenWithConfig();
                }
                BusinessEntiry load = new BusinessEntiry(this.dataManager, "Position", "PositionID");
                if (load.OpenByKeyValue(positionID))
                {
                    result = load.GetPropertyValue("DeptID");
                }
                else
                {
                    throw new Exception(String.Format("找不到职位[ID:{0}]的信息，请检查系统是否有此职位。", positionID));
                }

                if (result == string.Empty)
                {
                    throw new Exception(String.Format("找不到职位[ID:{0}]的所属部门，请检查系统是否有此职位。", positionID));
                }
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                try
                {
                    if (this.dataManager != null && !this.dataManager.IsClosed)
                    {
                        this.dataManager.Close();
                    }
                }
                catch
                {
                }
            }
        }
        #endregion

        [WebMethod(Description = "获取某个部门及其上级部门某种角色人员的信息")]
        public DataSet GetUserInfoByRole(string deptID, string roleName, int isLockCurrentDept)
        {
            try
            {
                if (this.dataManager.IsClosed)
                {
                    this.dataManager.OpenWithConfig();
                }

                string sql = string.Format(" EXEC PR_GetUserInfoByRole '{0}','{1}',{2}", deptID, roleName, isLockCurrentDept);
                DataSet ds = this.dataManager.SelectDataSet(sql);

                if (ds.Tables[0].Rows.Count == 0)
                {
                    throw new Exception(String.Format("找不到部门[ID:{0}],角色为[{1}]的人员信息。", deptID, roleName));
                }
                return ds;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                try
                {
                    if (this.dataManager != null && !this.dataManager.IsClosed)
                    {
                        this.dataManager.Close();
                    }
                }
                catch
                {
                }
            }
        }

        [WebMethod(Description = "获取某个部门及其上级部门某种职务人员的信息")]
        public DataSet GetUserInfoByJob(string deptID, string jobName, int isLockCurrentDept)
        {
            try
            {
                if (this.dataManager.IsClosed)
                {
                    this.dataManager.OpenWithConfig();
                }

                string sql = string.Format(" EXEC PR_GetUserInfoByJob '{0}','{1}',{2}", deptID, jobName, isLockCurrentDept);
                DataSet ds = this.dataManager.SelectDataSet(sql);

                if (ds.Tables[0].Rows.Count == 0)
                {
                    //throw new BusinessException((String.Format("找不到部门[ID:{0}],角色为[{1}]的人员信息。", deptID, jobName)));
                    throw new Exception(String.Format("找不到部门[ID:{0}],角色为[{1}]的人员信息。", deptID, jobName));
                }
                return ds;
            }
            catch (Exception ex)
            {
                //throw new BusinessException(ex.Message);
                throw ex;
            }
            finally
            {
                try
                {
                    if (this.dataManager != null && !this.dataManager.IsClosed)
                    {
                        this.dataManager.Close();
                    }
                }
                catch
                {
                }
            }
        }

        [WebMethod(Description = "获取某个部门及其上级部门某种职位人员的信息")]
        public DataSet GetUserInfoByPosition(string deptID, string jobName, int isLockCurrentDept, int isLockTree)
        {
            try
            {
                if (this.dataManager.IsClosed)
                {
                    this.dataManager.OpenWithConfig();
                }

                string sql = string.Format(" EXEC PR_GetUserInfoByPosition '{0}','{1}',{2},{3}", deptID, jobName, isLockCurrentDept, isLockTree);
                DataSet ds = this.dataManager.SelectDataSet(sql);

                if (ds.Tables[0].Rows.Count == 0)
                {
                    //throw new BusinessException((String.Format("找不到部门[ID:{0}],角色为[{1}]的人员信息。", deptID, jobName)));
                    if (isLockTree == 1)
                    {
                        throw new Exception(String.Format("找不到部门[ID:{0}],职位为[{1}]的人员信息。", deptID, jobName));
                    }
                    else
                    {
                        throw new Exception(String.Format("找不到职位为[{0}]的人员信息。", jobName));
                    }
                }
                return ds;
            }
            catch (Exception ex)
            {
                //throw new BusinessException(ex.Message);
                throw ex;
            }
            finally
            {
                try
                {
                    if (this.dataManager != null && !this.dataManager.IsClosed)
                    {
                        this.dataManager.Close();
                    }
                }
                catch
                {
                }
            }
        }




        [WebMethod(Description = "获取某个部门及其上级部门某种角色人员的信息")]
        public DataSet GetUserInfoByPower(string deptID, string roleName)
        {
            return new DataSet();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="propertyName"></param>
        /// <param name="setValue"></param>
        /// <param name="keyName"></param>
        /// <param name="keyValue"></param>
        [WebMethod(Description = "根据表的关键字段设置某个列的值")]
        public void SetPropertyValueByKeyValue(string tableName, string propertyName, string setValue, string keyName, string keyValue)
        {
            try
            {
                if (this.dataManager.IsClosed)
                {
                    this.dataManager.OpenWithConfig();
                }
                WF.Common.CentaUtil.SetPropertyValueByKeyValue(this.dataManager, tableName, propertyName, setValue, keyName, keyValue);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                try
                {
                    if (this.dataManager != null && !this.dataManager.IsClosed)
                    {
                        this.dataManager.Close();
                    }
                }
                catch
                {
                }
            }
        }

        [WebMethod(Description = "根据表的关键字段得到某个列的值")]
        public string GetPropertyValueByKeyValue(string tableName, string propertyName, string keyName, string keyValue)
        {
            try
            {
                if (this.dataManager.IsClosed)
                {
                    this.dataManager.OpenWithConfig();
                }
                return WF.Common.CentaUtil.GetPropertyValueByKeyValue(this.dataManager, tableName, propertyName, keyName, keyValue);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                try
                {
                    if (this.dataManager != null && !this.dataManager.IsClosed)
                    {
                        this.dataManager.Close();
                    }
                }
                catch
                {
                }
            }
        }


        [WebMethod(Description = "取得某个申请单某个审批步骤的审批备注值")]
        public string GetApprovalComment(string applyID, string activityName)
        {
            string comment = "";
            try
            {
                if (this.dataManager.IsClosed)
                {
                    this.dataManager.OpenWithConfig();
                }
                BusinessEntiry load = new BusinessEntiry(this.dataManager, "WF_AuditHistory", "ItemID");
                if (load.Open(string.Format(@" ItemID = (Select top 1 ItemID From WF_AuditHistory Where ApplyID = '{0}' And ActivityName='{1}' And ApprovalResult='通过')", applyID, activityName)))
                {
                    comment = load.GetPropertyValue("ApprovalComment");
                }

                return comment;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                try
                {
                    if (this.dataManager != null && !this.dataManager.IsClosed)
                    {
                        this.dataManager.Close();
                    }
                }
                catch
                {
                }
            }
        }

        /// <summary>
        /// 更新流程实例的最新状态

        /// </summary>
        /// <param name="applyID"></param>
        /// <param name="curWorkFlowStep"></param>
        /// <param name="actionType">动作类型，0：新增/更新，1：删除</param>
        /// <param name="curAuditor">当前审批人（可有多个，以分号分割）</param>
        [WebMethod]
        public void SyncActivityInstanceState(string applyID, string curWorkFlowStep, int actionType, string curAuditor)
        {
            try
            {
                this.dataManager.OpenWithConfig();

                if (actionType == 0)
                {
                    BusinessEntiry be = new BusinessEntiry(this.dataManager, "WF_ApplyInstanceStateDetail", "RowID");
                    if (be.Open(String.Format("ApplyID='{0}' and CurWorkFlowStep='{1}'", applyID, curWorkFlowStep)))
                    {
                        be.SetProperty("CurAuditor", curAuditor);
                    }
                    else
                    {
                        be = new BusinessEntiry(this.dataManager, "WF_ApplyInstanceStateDetail", "RowID");
                        be.SetProperty("RowID", System.Guid.NewGuid().ToString());
                        be.SetProperty("ApplyID", applyID);
                        be.SetProperty("CurWorkFlowStep", curWorkFlowStep);
                        be.SetProperty("CurAuditor", curAuditor);
                    }
                    be.Save();
                }
                else if (actionType == 1)
                {
                    string sql = "";
                    if (curWorkFlowStep.Trim() == "" || curWorkFlowStep.Trim().IndexOf("结束") > -1)
                    {
                        sql = String.Format("delete WF_ApplyInstanceStateDetail where ApplyID='{0}' ", applyID);
                    }
                    else
                    {
                        sql = String.Format("delete WF_ApplyInstanceStateDetail where ApplyID='{0}' and CurWorkFlowStep='{1}'", applyID, curWorkFlowStep);
                    }
                    dataManager.Execute(sql);
                }


            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                try
                {
                    if (this.dataManager != null && !this.dataManager.IsClosed)
                    {
                        this.dataManager.Close();
                    }
                }
                catch
                {
                }
            }
        }

        /// <summary>
        /// 更新流程实例的最新状态及当前步骤序号
        /// </summary>
        /// <param name="applyID"></param>
        /// <param name="curWorkFlowStep"></param>
        /// <param name="actionType"></param>
        /// <param name="curAuditor"></param>
        /// <param name="currentStepOrder"></param>
        [WebMethod]
        public void SyncActivityInstanceStateAndOrder(string applyID, string curWorkFlowStep, int actionType, string curAuditor, float currentStepOrder)
        {
            try
            {
                this.dataManager.OpenWithConfig();
                this.dataManager.BeginTransaction();

                if (actionType == 0)
                {
                    BusinessEntiry be = new BusinessEntiry(this.dataManager, "WF_ApplyInstanceStateDetail", "RowID");
                    if (be.Open(String.Format("ApplyID='{0}' and CurWorkFlowStep='{1}'", applyID, curWorkFlowStep)))
                    {
                        be.SetProperty("CurAuditor", curAuditor);
                    }
                    else
                    {
                        be = new BusinessEntiry(this.dataManager, "WF_ApplyInstanceStateDetail", "RowID");
                        be.SetProperty("RowID", System.Guid.NewGuid().ToString());
                        be.SetProperty("ApplyID", applyID);
                        be.SetProperty("CurWorkFlowStep", curWorkFlowStep);
                        be.SetProperty("CurAuditor", curAuditor);
                    }
                    be.Save(false);

                    //更新流程步骤序号
                    BusinessEntiry applyInstance = new BusinessEntiry(this.dataManager, "WF_ApplyInstance", "ApplyID");

                    if (applyInstance.OpenByKeyValue(applyID))
                    {
                        applyInstance.SetProperty("CurrentStepOrder", currentStepOrder.ToString());
                        applyInstance.Save(false);
                    }
                }
                else if (actionType == 1)
                {
                    string sql = String.Format("delete WF_ApplyInstanceStateDetail where ApplyID='{0}' and CurWorkFlowStep='{1}'", applyID, curWorkFlowStep);
                    dataManager.Execute(sql);
                }


                this.dataManager.CommitTransaction();
            }
            catch (Exception ex)
            {
                this.dataManager.RollbackTransaction();
                throw ex;
            }
            finally
            {
                try
                {
                    if (this.dataManager != null && !this.dataManager.IsClosed)
                    {
                        this.dataManager.Close();
                    }
                }
                catch
                {
                }
            }
        }

        /// <summary>
        /// 更新流程实例的最新步骤顺序号
        /// </summary>
        /// <param name="applyID"></param>
        /// <param name="currentStepOrder"></param>
        [WebMethod]
        public void UpdateApplyInstanceStepOrder(string applyID, float currentStepOrder)
        {
            try
            {
                this.dataManager.OpenWithConfig();
                //更新流程步骤序号
                BusinessEntiry applyInstance = new BusinessEntiry(this.dataManager, "WF_ApplyInstance", "ApplyID");

                if (applyInstance.OpenByKeyValue(applyID))
                {
                    applyInstance.SetProperty("CurrentStepOrder", currentStepOrder.ToString());
                    applyInstance.Save(!this.dataManager.IsBeginTrasaction);
                }


            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                try
                {
                    if (this.dataManager != null && !this.dataManager.IsClosed)
                    {
                        this.dataManager.Close();
                    }
                }
                catch
                {
                }
            }
        }

        /// <summary>
        /// 增加审批历史记录
        /// </summary>
        /// <param name="applyID">申请单ID</param>
        /// <param name="approvalXML">审批结果的XML</param>
        [WebMethod]
        public void AddAuditHistory(
            string applyID,
            string approvalXML)
        {
            try
            {
                this.dataManager.OpenWithConfig();
                DataSet ds = new DataSet();
                System.IO.StringReader sr = new System.IO.StringReader(this.TransferApprovalXml(approvalXML));
                ds.ReadXml(sr);

                Com.Object.Fields fs = new Com.Object.Fields();
                fs.LoadDataFromDataSet(ds, true);

                BusinessEntiry be = new BusinessEntiry(this.dataManager, "WF_AuditHistory", "RowID");
                be.SetProperty("RowID", System.Guid.NewGuid().ToString());
                be.SetProperty("ApplyID", applyID);
                be.SetProperty("UserName", fs.GetFieldValue("Name").Replace("@centaline.com.cn", ""));
                be.SetProperty("DomainAccount", fs.GetFieldValue("DomainAccount"));
                be.SetProperty("ApprovalResult", fs.GetFieldValue("ApprovalResult"));
                be.SetProperty("ApprovalComment", fs.GetFieldValue("ApprovalComment"));
                be.SetProperty("ApprovalDate", fs.GetFieldValue("ApprovalTime"));
                be.SetProperty("ActivityName", fs.GetFieldValue("ActivityName"));
                be.Save(!this.dataManager.IsBeginTrasaction);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                try
                {
                    if (this.dataManager != null && !this.dataManager.IsClosed)
                    {
                        this.dataManager.Close();
                    }
                }
                catch
                {
                }
            }
        }


        [WebMethod(Description = "生成到财务代收代付系统的待推送记录")]
        public void PushAccountFlowDataToSupply(string flowID)
        {
            try
            {
                if (this.dataManager.IsClosed)
                {
                    this.dataManager.OpenWithConfig();
                }

                string sql = string.Format(" EXEC PR_FIN_InsertSupplyRecordByFlowID '{0}'", flowID);
                this.dataManager.Execute(sql);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                try
                {
                    if (this.dataManager != null && !this.dataManager.IsClosed)
                    {
                        this.dataManager.Close();
                    }
                }
                catch
                {
                }
            }
        }

        [WebMethod(Description = "生成补备用金到财务代收代付系统的待推送记录")]
        public void PushImprestApplyIntoSupplyRecord(string itemID)
        {
            try
            {
                if (this.dataManager.IsClosed)
                {
                    this.dataManager.OpenWithConfig();
                }

                string sql = string.Format(" EXEC PR_FIN_PushImprestApplyIntoSupplyRecord '{0}'", itemID);
                this.dataManager.Execute(sql);

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                try
                {
                    if (this.dataManager != null && !this.dataManager.IsClosed)
                    {
                        this.dataManager.Close();
                    }
                }
                catch
                {
                }
            }
        }


        //TEST
        [WebMethod(Description = "检查是否某个流程的顶级职位，用于判断是否跳出逐步审批(0否，1是)")]
        public int CheckIsTopPosition_T(string applyType, int jobLevel, string positionID)
        {
            int deptKind = this.Get_DeptKindByPositionID(positionID);
            return Check_IsTopPosition_T(applyType, deptKind, jobLevel, positionID);
        }
        private int Check_IsTopPosition_T(string applyType, int deptKind, int jobLevel, string positionID)
        {
            int result = 0;
            try
            {
                if (this.dataManager.IsClosed)
                {
                    this.dataManager.OpenWithConfig();
                }
                string sql = string.Format(" EXEC PR_CheckIsTopPosition_Test '{0}','{1}','{2}','{3}'", applyType, deptKind.ToString(), jobLevel.ToString(), positionID);
                DataSet ds = this.dataManager.SelectDataSet(sql);

                if (ds.Tables[0].Rows.Count == 0)
                {
                    result = 0;
                }
                else
                {
                    result = int.Parse(ds.Tables[0].Rows[0]["istop"].ToString());
                    if (result > 1)
                    {
                        result = 1;
                    }
                }
                return result;
            }
            catch (Exception ex)
            {
                return -1;
            }
            finally
            {
                try
                {
                    if (this.dataManager != null && !this.dataManager.IsClosed)
                    {
                        this.dataManager.Close();
                    }
                }
                catch
                {
                }
            }
        }

        [WebMethod(Description = "获取上级人员的信息，根据职位ID")]
        public DataSet GetManagerByPositionID_T(string positionID)
        {
            if (this.dataManager.IsClosed)
            {
                this.dataManager.OpenWithConfig();
            }

            string sql = string.Format(" EXEC PR_GetManagerByPositionID_Test '{0}'", positionID);
            DataSet ds = new DataSet();
            ds = this.dataManager.SelectDataSet(sql);
            return ds;
        }

        [WebMethod(Description = "获取上级人员的信息，根据组织单元ID")]
        public DataSet GetManagerByDeptID_T(string deptID)
        {
            try
            {
                if (this.dataManager.IsClosed)
                {
                    this.dataManager.OpenWithConfig();
                }

                string sql = string.Format(" EXEC PR_GetManagerByDeptID_Test '{0}'", deptID);
                DataSet ds = this.dataManager.SelectDataSet(sql);

                if (ds.Tables.Count == 0)
                {
                    //throw new Exception(String.Format("找不到部门[ID:{0}]的上级人员信息。", deptID));
                }
                return ds;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                try
                {
                    if (this.dataManager != null && !this.dataManager.IsClosed)
                    {
                        this.dataManager.Close();
                    }
                }
                catch
                {
                }
            }
        }

        [WebMethod(Description = "获取某个部门及其上级部门某种角色人员的信息")]
        public DataSet GetUserInfoByRole_T(string deptID, string roleName, int isLockCurrentDept)
        {
            try
            {
                if (this.dataManager.IsClosed)
                {
                    this.dataManager.OpenWithConfig();
                }

                string sql = string.Format(" EXEC PR_GetUserInfoByRole_Test '{0}','{1}',{2}", deptID, roleName, isLockCurrentDept);
                DataSet ds = this.dataManager.SelectDataSet(sql);

                if (ds.Tables.Count == 0)
                {
                    //throw new Exception(String.Format("找不到部门[ID:{0}],角色为[{1}]的人员信息。", deptID, roleName));
                }
                return ds;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                try
                {
                    if (this.dataManager != null && !this.dataManager.IsClosed)
                    {
                        this.dataManager.Close();
                    }
                }
                catch
                {
                }
            }
        }

        [WebMethod(Description = "获取某个部门及其上级部门某种职务人员的信息")]
        public DataSet GetUserInfoByJob_T(string deptID, string jobName, int isLockCurrentDept)
        {
            try
            {
                if (this.dataManager.IsClosed)
                {
                    this.dataManager.OpenWithConfig();
                }

                string sql = string.Format(" EXEC PR_GetUserInfoByJob_Test '{0}','{1}',{2}", deptID, jobName, isLockCurrentDept);
                DataSet ds = this.dataManager.SelectDataSet(sql);

                if (ds.Tables[0].Rows.Count == 0)
                {
                    //throw new BusinessException((String.Format("找不到部门[ID:{0}],角色为[{1}]的人员信息。", deptID, jobName)));
                    throw new Exception(String.Format("找不到部门[ID:{0}],角色为[{1}]的人员信息。", deptID, jobName));
                }
                return ds;
            }
            catch (Exception ex)
            {
                //throw new BusinessException(ex.Message);
                throw ex;
            }
            finally
            {
                try
                {
                    if (this.dataManager != null && !this.dataManager.IsClosed)
                    {
                        this.dataManager.Close();
                    }
                }
                catch
                {
                }
            }
        }

        [WebMethod(Description = "获取某个部门及其上级部门某种职位人员的信息")]
        public DataSet GetUserInfoByPosition_T(string deptID, string jobName, int isLockCurrentDept, int isLockTree)
        {
            try
            {
                if (this.dataManager.IsClosed)
                {
                    this.dataManager.OpenWithConfig();
                }

                string sql = string.Format(" EXEC PR_GetUserInfoByPosition_Test '{0}','{1}',{2},{3}", deptID, jobName, isLockCurrentDept, isLockTree);
                DataSet ds = this.dataManager.SelectDataSet(sql);

                if (ds.Tables[0].Rows.Count == 0)
                {
                    //throw new BusinessException((String.Format("找不到部门[ID:{0}],角色为[{1}]的人员信息。", deptID, jobName)));
                    if (isLockTree == 1)
                    {
                        throw new Exception(String.Format("找不到部门[ID:{0}],职位为[{1}]的人员信息。", deptID, jobName));
                    }
                    else
                    {
                        throw new Exception(String.Format("找不到职位为[{0}]的人员信息。", jobName));
                    }
                }
                return ds;
            }
            catch (Exception ex)
            {
                //throw new BusinessException(ex.Message);
                throw ex;
            }
            finally
            {
                try
                {
                    if (this.dataManager != null && !this.dataManager.IsClosed)
                    {
                        this.dataManager.Close();
                    }
                }
                catch
                {
                }
            }
        }
        //结束TEST

        //更新业务流程信息
        /// <summary>
        /// 更新流程实例的最新步骤顺序号
        /// </summary>
        /// <param name="applyID"></param>
        /// <param name="currentStepOrder"></param>
        [WebMethod]
        public void AutoGenEmpNoToApply(string applyID)
        {
            try
            {
                this.dataManager.OpenWithConfig();
                //更新
                BusinessEntiry be = new BusinessEntiry(this.dataManager, "EmployeeAdjustApply", "RowID");
                if (be.Open(string.Format(@" Rowid = (select top 1 bizdataid from wf_applyinstance where applyid = '{0}') ", applyID)))
                {
                    //如果工号为空才要获取 -bak
                    //if (be.GetPropertyValue("EmpNo") == string.Empty)
                    //{
                    //    HRCoder hrCoder = new HRCoder();
                    //    string empNo = hrCoder.NewEmployeeNumber();
                    //    be.SetProperty("EmpCode", empNo);
                    //    be.Save(!this.dataManager.IsBeginTrasaction);
                    //}
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                try
                {
                    if (this.dataManager != null && !this.dataManager.IsClosed)
                    {
                        this.dataManager.Close();
                    }
                }
                catch
                {
                }
            }
        }
    }
}
